package vn.aptech.leaveManagementSystem.view;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import vn.aptech.leaveManagementSystem.business.UserManager;
import vn.aptech.leaveManagementSystem.business.dto.UserLoginDTO;
import vn.aptech.leaveManagementSystem.business.dto.UserLoginInfo;
import vn.aptech.leaveManagementSystem.view.admin.AdminForm;
import vn.aptech.leaveManagementSystem.view.director.DirectorMainForm;
import vn.aptech.leaveManagementSystem.view.manager.DemoManagerMainForm;
import vn.aptech.leaveManagementSystem.view.personnel.DemoPersonnelMainForm;

public class DemoLgFr extends JFrame{

	//private JFrame frLogin;
	private JTextField txtfUserName;
	private JPasswordField pwdfPassword;
	private JButton btnLogin;
	private JButton btnReset;
	private JButton btnCancel;
	protected JFrame thisJFrame=this;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the application.
	 */
	public DemoLgFr() {
		initGUI();
		initAction();
		initData();
		//initialize();
	}

	private void initData() {
		
		
	}

	private void initAction() {
		
		btnLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				UserLoginDTO user=new UserLoginDTO();
				user.setUserName(txtfUserName.getText());
				user.setPassword(new String(pwdfPassword.getPassword()));
				
				try {
					UserManager um=new UserManager();
					boolean test=um.checkLogin(user);
					if (test==true)
					{
						thisJFrame.dispose();
						
						if (UserLoginInfo.getUserInfo().getPositionID()==1)
						{
							AdminForm dirForm=new AdminForm();
							dirForm.setVisible(true);
						}
						
						if (UserLoginInfo.getUserInfo().getPositionID()==2)
						{
							DirectorMainForm dirForm=new DirectorMainForm();
							dirForm.setVisible(true);
						}
						if(UserLoginInfo.getUserInfo().getPositionID()==3)
						{
							DemoManagerMainForm managerform=new DemoManagerMainForm();
							managerform.setVisible(true);
						}
						if(UserLoginInfo.getUserInfo().getPositionID()==4)
						{
							DemoPersonnelMainForm managerform=new DemoPersonnelMainForm();
							managerform.setVisible(true);
						}
						
					}
					else
					{
						JOptionPane.showMessageDialog(thisJFrame, "UserName or Password incorrect!");
						txtfUserName.requestFocusInWindow();
					}
				} catch (SQLException ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(thisJFrame, ex.getMessage());
				}	
			}
		});
		
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtfUserName.setText(null);
				pwdfPassword.setText(null);
				
				txtfUserName.requestFocus();
				txtfUserName.getCursor();
			}
		});
		
		
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		txtfUserName.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent ke) {
				if (ke.getKeyCode()==KeyEvent.VK_ENTER)
				{
					pwdfPassword.requestFocusInWindow();
				}
			}
		});
		
		pwdfPassword.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent ke) {
				if (ke.getKeyCode()==KeyEvent.VK_ENTER)
				{
					btnLogin.doClick();
				}	
			}
		});
	}

	/**
	 * Initialize the contents of the frame.
	 */
	
	private void initGUI() {
		//frLogin = new JFrame();
		setTitle("Login");
		getContentPane().setBackground(SystemColor.inactiveCaptionText);
		setBounds(100, 100, 406, 246);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		JLabel lblBackground = new JLabel("");
		lblBackground.setBounds(0, 0, 398, 73);
		lblBackground.setIcon(new ImageIcon("backgrounds/Background1.png"));
		getContentPane().add(lblBackground);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(25, 73, 373, 125);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblUserName = new JLabel("UserName:");
		lblUserName.setBounds(10, 17, 68, 14);
		panel.add(lblUserName);
		
		txtfUserName = new JTextField();
		txtfUserName.setBounds(78, 14, 286, 20);
		panel.add(txtfUserName);
		txtfUserName.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(10, 44, 68, 17);
		panel.add(lblPassword);
		
		pwdfPassword = new JPasswordField();
		pwdfPassword.setBounds(78, 42, 286, 20);
		panel.add(pwdfPassword);
		
		btnLogin = new JButton("Login");
		btnLogin.setBounds(78, 82, 91, 23);
		btnLogin.setIcon(new ImageIcon("icons/png/16x16/Login.png"));
		panel.add(btnLogin);
		
		btnReset=new JButton("Reset");
		btnReset.setBounds(173, 82, 90, 23);
		btnReset.setIcon(new ImageIcon("icons/gif/16x16/Redo.gif"));
		panel.add(btnReset);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(267, 82, 97, 23);
		btnCancel.setIcon(new ImageIcon("icons/gif/16x16/Cancel.gif"));
		panel.add(btnCancel);
		
	}
	
	public static void main(String[] args) {
		DemoLgFr demo=new DemoLgFr();
		demo.setVisible(true);
	}
	
}
